<?php
namespace app\admin\controller;
use app\admin\model\Airport;
use app\admin\model\Business;
use app\admin\model\Citys;
use app\admin\model\ListContent;
use app\admin\model\ListVedio;
use app\admin\model\Region;
use app\admin\model\Subway;
use think\Request;

class Selected extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index(){
        return $this->fetch('public/help');
    }

    public function selectedList(Request $request){
        $db = new ListContent();
        $rs = $db->getAllPage();
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function selectedInfo(Request $request){
        $db1 = new ListContent();
        if($request->get()){
            $data = $request->get();
            $where['id'] = $data['id'];
            $rs = $db1->getOne($where);
            //酒店信息
            $this->assign('rs',$rs);
            $where['id'] = ['neq',0];
            //城市
            $citys = new Citys();
            $citysList = $citys->getAll($where);
            $this->assign('citysList',$citysList);
            //城区
            $region = new Region();
            $regionList = $region->getAll($where);
            $this->assign('regionList',$regionList);
            //商业区
            $business = new Business();
            $businessList = $business->getAll($where);
            $this->assign('businessList',$businessList);
            //机场
            $airport = new Airport();
            $airportList = $airport->getAll($where);
            $this->assign('airportList',$airportList);
            //地铁
            $subway = new Subway();
            $subwayList = $subway->getAll($where);
            $this->assign('subwayList',$subwayList);
            return $this->fetch();
        }else{
            $data = $request->post();
            $where1['id'] = $data['id'];
            $editData1['title'] = $data['title'];
            $editData1['detail'] = $data['detail'];
            $editData1['number_min'] = $data['number_min'];
            $editData1['number_max'] = $data['number_max'];
            $editData1['area_min'] = $data['area_min'];
            $editData1['area_max'] = $data['area_max'];
            $editData1['guest'] = $data['guest'];
            $editData1['banquet'] = $data['banquet'];
            $editData1['tese'] = $data['tese'];
            $editData1['list_sort'] = $data['list_sort'];
            $editData1['index_sort'] = $data['index_sort'];
            $editData1['location'] = $data['location'];
            $editData1['image'] = $data['image'];
            $editData1['city_id'] = $data['city_id'];
            $editData1['region'] = $data['region'];
            $editData1['business'] = $data['business'];
            $editData1['subway'] = $data['subway'];

            $db2 = new ListVedio();
            $where2['lid'] = $data['id'];
            $editData2['vedio_image'] = $data['vo_image'];
            $editData2['vedio_link'] = $data['vo_vedio'];
            $editData2['photo'] = $data['vo_photo'];

            if(!$db1->edit($where1,$editData1) and !$db2->edit($where2,$editData2)){
                return $this->error('修改失败');
            }else{
                return $this->success('修改成功');
            }
        }
    }

    public function addSelected(Request $request){
        $db1 = new ListContent();
        if ($request->post()){
            $data = $request->post();
            $addData1['title'] = $data['title'];
            $addData1['detail'] = $data['detail'];
            $addData1['number_min'] = $data['number_min'];
            $addData1['number_max'] = $data['number_max'];
            $addData1['area_min'] = $data['area_min'];
            $addData1['area_max'] = $data['area_max'];
            $addData1['guest'] = $data['guest'];
            $addData1['banquet'] = $data['banquet'];
            $addData1['tese'] = $data['tese'];
            $addData1['list_sort'] = $data['list_sort'];
            $addData1['index_sort'] = $data['index_sort'];
            $addData1['location'] = $data['location'];
            $addData1['image'] = $data['image'];
            $addData1['city_id'] = $data['city_id'];
            $addData1['region'] = $data['region'];
            $addData1['business'] = $data['business'];
            $addData1['subway'] = $data['subway'];
            if($db1->add($addData1)){
                $db2 = new ListVedio();
                $addData2['lid'] = $db1->getLastInsID();
                $addData2['vedio_image'] = $data['vo_image'];
                $addData2['vedio_link'] = $data['vo_vedio'];
                $addData2['photo'] = $data['vo_photo'];
                if($db2->add($addData2)){
                    return $this->success('添加成功');
                }else{
                    return $this->error('添加失败');
                }
            }else{
                return $this->error('添加失败');
            }
        }else{
            //城市
            $citys = new Citys();
            $where = [];
            $citysList = $citys->getAll($where);
            $this->assign('citysList',$citysList);
            //城区
            $region = new Region();
            $regionList = $region->getAll($where);
            $this->assign('regionList',$regionList);
            //商业区
            $business = new Business();
            $businessList = $business->getAll($where);
            $this->assign('businessList',$businessList);
            //机场
            $airport = new Airport();
            $airportList = $airport->getAll($where);
            $this->assign('airportList',$airportList);
            //地铁
            $subway = new Subway();
            $subwayList = $subway->getAll($where);
            $this->assign('subwayList',$subwayList);
            return $this->fetch();
        }

    }
}